package com.tmc.service.baseinfo;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.dao.impl.EthClientDaoImpl;
import com.tmc.entity.common.EthClient;
import com.tmc.util.ObjectUtil;

@Service
@Transactional
public class EthClientServiceImpl {

	@Autowired
	private EthClientDaoImpl dao;
	
	private Log logger = LogFactory.getLog(this.getClass());
	
	public Pagination<EthClient> findByCon(ContextInfo context, EthClient cond, int pageIndex, int pageSize) {
		Pagination<EthClient> result = dao.findByCon(context, cond, pageIndex, pageSize);
		return result;
	}
	
	public EthClient getById(ContextInfo context ,Long clientId){
		if(clientId == null){
			return null;
		}
		EthClient result = dao.getById(clientId);
		return result;	
	}
	
	public CommonResult save(ContextInfo context, EthClient client) {
		CommonResult check = this.check(client);
		if (!check.isSuccess()) {
			return check;
		}
		// 保存
		if (client.getClientId() == null) {
			EthClient dbEthClient = dao.getByClientName(context, client.getClientName());
			if (dbEthClient != null) {
				return CommonResult.makeFail("商户名已存在");
			}			
			ObjectUtil.setBaseInfo(client, context);
			client.setClientCode("SZTH");
			client.setCreditAvailable(client.getCreditLine());
			client.setCreditBalance((double)client.getCreditLine());
			dao.insert(client);
		} else {
			// 编辑
			EthClient dbEthClient = dao.getByClientName(context, client.getClientName());
			if (dbEthClient != null && client.getClientId() != dbEthClient.getClientId()) {
				return CommonResult.makeFail("商户名已存在");
			}
			try {
				dbEthClient = dao.getById(client.getClientId());
				//设置可用额度
				client.setCreditAvailable(client.getCreditLine()-dbEthClient.getCreditLine()+dbEthClient.getCreditAvailable());
				client.setCreditBalance(client.getCreditLine()-dbEthClient.getCreditLine()+dbEthClient.getCreditBalance());
				if (dbEthClient != null) {
					ObjectUtil.copyProperties(client, dbEthClient,  
							new String[] { "createdBy", "creationDate", "clientId", "enabledFlag", "clientCode"});
				}
				ObjectUtil.updateBaseInfo(dbEthClient, context);
				dao.update(dbEthClient);
			} catch (Exception e) {
				logger.error("拷贝对象错误！", e);
				return CommonResult.makeFail("拷贝对象错误！");
			}
		}
		return CommonResult.makeSuccess();		
	}
	
	public List<EthClient> search(String text){
		List<EthClient> clientList= new ArrayList<EthClient>();
		if(ObjectUtil.noText(text)){
			List<EthClient> list = dao.findByClientName("");
			for (EthClient ethClient : list) {
				clientList.add(ethClient);
				if(clientList.size() >=5){
					return clientList;
				}
			}
			return clientList;
		}
	
		List<EthClient> list = dao.findByClientName(text);		
			for (EthClient ethClient : list) {
				clientList.add(ethClient);
				if(clientList.size() >=5){
					return clientList;
				}
			}
		return clientList;		
	}


	private CommonResult check(EthClient client) {
//		if (!ObjectUtil.hasText(client.getClientCode())) {
//			return CommonResult.makeFail("编码不能为空！");
//		}
		if (!ObjectUtil.hasText(client.getClientName())) {
			return CommonResult.makeFail("名字不能为空！");
		}
		return CommonResult.makeSuccess();
	}
}
